package com.fancy.projecty.B_流程实例;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipInputStream;

/**
 * 启动流程实例，动态设置assignee
 * <p>
 * 启动流程实例之前 如果修改了bpmn的话,需要重新的去部署
 * 需要先部署流程
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class K_启动流程实例同时动态设置BusinessKey的值 {


	@Autowired
	/*任务管理,可以做任务的查询的*/
	private TaskService taskService;
	/*历史记录的查询*/
	@Autowired
	private HistoryService historyService;


	@Autowired
	/*管理流程定义,部署相关的Service*/
	private RepositoryService repositoryService;


	@Autowired
	/*执行管理、包括启动、推进、删除流程实例等操作*/
	private RuntimeService runtimeService;

	/**
	 * processDefinitionKey 是 部署的流程的key  (act_re_procdef 表的 KEY_ 字段)
	 * 执行成功之后 ACT_RU 的很多表会多数据
	 */
	@Test
	public void c_启动流程实例同时动态设置BusinessKey的值() {


		/*3.设置assignee的取值   用户可以在界面上设置流程的执行人
		 * 执行任务的时候可以动态的去设置流程的执行人,这个值可以是在页面上获取的,
		 * 也可以是从数据库查询出来填充上去的.
		 *
		 *需要注意,key必须要和bpmn上的值对应才行
		 * */
		Map<String, Object> map = new HashMap<>();
		map.put("assignee0", "zhangsan");
		map.put("assignee1", "lishi");
		map.put("assignee2", "wangwu");
		//4.启动流程实例，同时还要设置流程定义的assignee的值
		ProcessInstance holiday2 = this.runtimeService.startProcessInstanceByKey("holiday2", map);

		//5.输出
		System.out.println(holiday2.getName());
	}
	/*执行成功之后这些表会出现内容
	==>  Preparing: update ACT_GE_PROPERTY SET REV_ = ?, VALUE_ = ? where NAME_ = ? and REV_ = ?
==> Parameters: 9(Integer), 20001(String), next.dbid(String), 8(Integer)
<==    Updates: 1
==>  Preparing: insert into ACT_HI_VARINST (ID_, PROC_INST_ID_, EXECUTION_ID_, TASK_ID_, NAME_, REV_, VAR_TYPE_, BYTEARRAY_ID_, DOUBLE_, LONG_ , TEXT_, TEXT2_, CREATE_TIME_, LAST_UPDATED_TIME_) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) , (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) , (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
==> Parameters: 17503(String), 17501(String), 17501(String), null, assignee0(String), 0(Integer), string(String), null, null, null, zhangsan(String), null, 2020-01-02 13:35:17.89(Timestamp), 2020-01-02 13:35:17.89(Timestamp), 17504(String), 17501(String), 17501(String), null, assignee2(String), 0(Integer), string(String), null, null, null, wangwu(String), null, 2020-01-02 13:35:17.891(Timestamp), 2020-01-02 13:35:17.891(Timestamp), 17505(String), 17501(String), 17501(String), null, assignee1(String), 0(Integer), string(String), null, null, null, lishi(String), null, 2020-01-02 13:35:17.891(Timestamp), 2020-01-02 13:35:17.891(Timestamp)
<==    Updates: 3
==>  Preparing: insert into ACT_HI_TASKINST ( ID_, PROC_DEF_ID_, PROC_INST_ID_, EXECUTION_ID_, NAME_, PARENT_TASK_ID_, DESCRIPTION_, OWNER_, ASSIGNEE_, START_TIME_, CLAIM_TIME_, END_TIME_, DURATION_, DELETE_REASON_, TASK_DEF_KEY_, FORM_KEY_, PRIORITY_, DUE_DATE_, CATEGORY_, TENANT_ID_ ) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
==> Parameters: 17507(String), holiday2:1:15004(String), 17501(String), 17501(String), 填写请假单(String), null, null, null, lishi(String), 2020-01-02 13:35:17.906(Timestamp), null, null, null, null, _4(String), null, 50(Integer), null, null, (String)
<==    Updates: 1
==>  Preparing: insert into ACT_HI_PROCINST ( ID_, PROC_INST_ID_, BUSINESS_KEY_, PROC_DEF_ID_, START_TIME_, END_TIME_, DURATION_, START_USER_ID_, START_ACT_ID_, END_ACT_ID_, SUPER_PROCESS_INSTANCE_ID_, DELETE_REASON_, TENANT_ID_, NAME_ ) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
==> Parameters: 17501(String), 17501(String), null, holiday2:1:15004(String), 2020-01-02 13:35:17.89(Timestamp), null, null, null, _2(String), null, null, null, (String), null
<==    Updates: 1
==>  Preparing: insert into ACT_HI_ACTINST ( ID_, PROC_DEF_ID_, PROC_INST_ID_, EXECUTION_ID_, ACT_ID_, TASK_ID_, CALL_PROC_INST_ID_, ACT_NAME_, ACT_TYPE_, ASSIGNEE_, START_TIME_, END_TIME_, DURATION_, TENANT_ID_ ) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) , (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
==> Parameters: 17502(String), holiday2:1:15004(String), 17501(String), 17501(String), _2(String), null, null, StartEvent(String), startEvent(String), null, 2020-01-02 13:35:17.89(Timestamp), 2020-01-02 13:35:17.903(Timestamp), 13(Long), (String), 17506(String), holiday2:1:15004(String), 17501(String), 17501(String), _4(String), 17507(String), null, 填写请假单(String), userTask(String), lishi(String), 2020-01-02 13:35:17.904(Timestamp), null, null, (String)
<==    Updates: 2
==>  Preparing: insert into ACT_HI_IDENTITYLINK (ID_, TYPE_, USER_ID_, GROUP_ID_, TASK_ID_, PROC_INST_ID_) values (?, ?, ?, ?, ?, ?)
==> Parameters: 17508(String), participant(String), lishi(String), null, null, 17501(String)
<==    Updates: 1
==>  Preparing: insert into ACT_RU_EXECUTION (ID_, REV_, PROC_INST_ID_, BUSINESS_KEY_, PROC_DEF_ID_, ACT_ID_, IS_ACTIVE_, IS_CONCURRENT_, IS_SCOPE_,IS_EVENT_SCOPE_, PARENT_ID_, SUPER_EXEC_, SUSPENSION_STATE_, CACHED_ENT_STATE_, TENANT_ID_, NAME_) values ( ?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
==> Parameters: 17501(String), 17501(String), null, holiday2:1:15004(String), _4(String), true(Boolean), false(Boolean), true(Boolean), false(Boolean), null, null, 1(Integer), 2(Integer), (String), null
<==    Updates: 1
==>  Preparing: insert into ACT_RU_TASK (ID_, REV_, NAME_, PARENT_TASK_ID_, DESCRIPTION_, PRIORITY_, CREATE_TIME_, OWNER_, ASSIGNEE_, DELEGATION_, EXECUTION_ID_, PROC_INST_ID_, PROC_DEF_ID_, TASK_DEF_KEY_, DUE_DATE_, CATEGORY_, SUSPENSION_STATE_, TENANT_ID_, FORM_KEY_) values (?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
==> Parameters: 17507(String), 填写请假单(String), null, null, 50(Integer), 2020-01-02 13:35:17.904(Timestamp), null, lishi(String), null, 17501(String), 17501(String), holiday2:1:15004(String), _4(String), null, null, 1(Integer), (String), null
<==    Updates: 1
==>  Preparing: insert into ACT_RU_IDENTITYLINK (ID_, REV_, TYPE_, USER_ID_, GROUP_ID_, TASK_ID_, PROC_INST_ID_, PROC_DEF_ID_) values (?, 1, ?, ?, ?, ?, ?, ?)
==> Parameters: 17508(String), participant(String), lishi(String), null, null, 17501(String), null
<==    Updates: 1
==>  Preparing: INSERT INTO ACT_RU_VARIABLE (ID_, REV_, TYPE_, NAME_, PROC_INST_ID_, EXECUTION_ID_, TASK_ID_, BYTEARRAY_ID_, DOUBLE_, LONG_ , TEXT_, TEXT2_) VALUES (?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) , (?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) , (?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
==> Parameters: 17503(String), string(String), assignee0(String), 17501(String), 17501(String), null, null, null, null, zhangsan(String), null, 17504(String), string(String), assignee2(String), 17501(String), 17501(String), null, null, null, null, wangwu(String), null, 17505(String), string(String), assignee1(String), 17501(String), 17501(String), null, null, null, null, lishi(String), null
<==    Updates: 3
Closing JDBC Connection [Transaction-aware proxy for target Connection [ProxyConnection[PooledConnection[com.mysql.jdbc.JDBC4Connection@14ed7ddf]]]]
null
	*
	* */


	/**
	 * 部署流程
	 * 部署成功之后会生成25张表
	 * 同时 act_re_deployment 会有一条新的记录,
	 * act_re_procdef  也会生成一个新的记录
	 * 这个记录是你当前部署成功的这个流程的信息
	 */
	@Test
	public void c_1单个文件部署流程() {
		//得到repositoryService 就可以操作部署相关的操作了.
		//1. 添加资源, png资源可以不添加,但是bpmn资源是必须要添加的
		Deployment deployment = this.repositoryService.createDeployment()
				.addClasspathResource("diagram/holiday2.bpmn")
//				.addClasspathResource("diagram/holiday2.png")
				//给流程添加名字
				.name("请假申请单流程2")
				//开始部署
				.deploy();
		System.out.println(deployment.getName());  //请假申请单流程2
		System.out.println(deployment.getId());  // 12501
	}

	/**
	 * 这个 参数是  act_re_deployment 表的id字段
	 * 或者是 act_re_procdef 表的 act_re_procdef 字段
	 */
	@Test
	public void c_删除部署的流程() {
//3.执行删除流程定义  参数代表流程部署的id
		repositoryService.deleteDeployment("12501");
	}


}
